package com.aaa.controller;

import com.aaa.entity.Album;
import com.aaa.entity.Approval;
import com.aaa.entity.Voice;
import com.aaa.service.AlbumService;
import com.aaa.service.ApprovalService;
import com.aaa.service.VoiceService;
import com.aaa.util.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("approval")
public class ApprovalController {
    @Resource
    VoiceService voiceService;
    @Resource
    AlbumService albumService;
    @Resource
    ApprovalService approvalService;

    @RequestMapping("selectApproval")
    @ResponseBody
    public PageInfo select(Integer pageNum, Integer pageSize, String aname) {
        if(aname!=null){
            //模糊查询专辑名根据
            PageHelper.startPage(pageNum,pageSize);
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.like("aname",aname);
            List<Approval> list = approvalService.list(queryWrapper);
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }else {
            //查询全部
            PageHelper.startPage(pageNum,pageSize);
            List<Approval> list = approvalService.list();
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }
    }
    //向审批表添加数据
    @RequestMapping("approvalAdd")
    @ResponseBody
    public boolean voiceAdd(Approval approval,Integer state) {
        approval.setDate(new Date());
        //添加审批信息
        if(approval.getRemark()==null||approval.getRemark()==""){
            approval.setRemark("通过");
        }
        boolean save = approvalService.save(approval);
        if(save==true){
            Album album=new Album();
            album.setAname(approval.getAlbname());
            album.setState(state);
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("aname",album.getAname());
            albumService.update(album,updateWrapper);
        }
        return save;
    }
    //向审批表批量添加数据
    @RequestMapping("approvalAdds")
    @ResponseBody
    public boolean voiceAdds(String str,String albnames,Integer state,String aname) {
        Approval approval=new Approval();
        boolean save=false;
        boolean update=false;
        String[] albname=albnames.split(",");
        for (int i=0;i<albname.length;i++){
            approval.setDate(new Date());
            approval.setRemark("通过");
            approval.setAname(aname);
            approval.setAlbname(albname[i]);
            save = approvalService.save(approval);
            if(save==true){
                Album album=new Album();
                album.setAname(approval.getAlbname());
                album.setState(state);
                UpdateWrapper updateWrapper=new UpdateWrapper();
                updateWrapper.eq("aname",album.getAname());
               update = albumService.update(album, updateWrapper);
            }
        }
        //添加审批信息
        boolean resule = save == update;
        return resule;
    }

}
